﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using INSTA.BankApplication.DAL;
using INSTA.Library;
using NUnit.Framework;


namespace INSTA.BankApplication.DAL.Tests
{

    public abstract class Test_Persistence_BaseClass
    {
        [SetUp]
        public abstract void SetUp();

        [Test]
        public void Test_Method_Save_ForInsert()
        {
            var p = new MockPerson();

            p.Name = "TestName";
            p.FirstName = "TestFirstName";
            p.YearOfBirth = 1900;


            // Try to perform an INSERT into the database.
            bool success = p.Save();

            bool result = success && p.Id != -1;

            p.Delete();

            Assert.IsTrue(result);

        }


        [Test]
        public void Test_Method_Save_ForUpdate()
        {
            var p = new MockPerson();

            p.Name = "TestName";
            p.FirstName = "TestFirstName";
            p.YearOfBirth = 1900;


            // Try to perform an INSERT into the database.
            bool success = p.Save();

            // Let's get the auto-generated Id back from the database.
            int savedPersonId = p.Id;


            var p2 = new MockPerson(savedPersonId);

            bool result = p.Id == p2.Id &&
                          p.Name == p2.Name &&
                          p.FirstName == p2.FirstName &&
                          p.YearOfBirth == p2.YearOfBirth;


            p.Delete();
            Assert.IsTrue(result);
        }


        [Test]
        public void Test_Method_Load_ForSuccess()
        {
            var p = new MockPerson();

            p.Name = "TestName";
            p.FirstName = "TestFirstName";
            p.YearOfBirth = 1900;


            // Try to perform an INSERT into the database.
            bool success = p.Save();

            // Let's get the auto-generated Id back from the database.
            int savedPersonId = p.Id;


            var p2 = new MockPerson(savedPersonId);

            bool result = p.Id == p2.Id &&
                          p.Name == p2.Name &&
                          p.FirstName == p2.FirstName &&
                          p.YearOfBirth == p2.YearOfBirth;


            p.Delete();
            Assert.IsTrue(result);

        }

        [Test]
        public void Test_Method_Load_ForException()
        {
            Assert.Throws<KeyNotFoundException>(() => new MockPerson(-10));

        }


        [Test]
        public void Test_Method_DeleteForSuccess()
        {
            var p = new MockPerson();

            p.Name = "TestName";
            p.FirstName = "TestFirstName";
            p.YearOfBirth = 1900;


            // Try to perform an INSERT into the database.
            bool success = p.Save();

            // Let's get the auto-generated Id back from the database.
            int savedPersonId = p.Id;

            p.Delete();

            Assert.Throws<KeyNotFoundException>(() => new MockPerson(savedPersonId));
        }


        [Test]
        public void Test_Method_DeleteForException()
        {
            var p = new MockPerson();

            p.Name = "TestName";
            p.FirstName = "TestFirstName";
            p.YearOfBirth = 1900;


            // Try to perform an INSERT into the database.
            p.Save();


            p.Delete();

            // Assert that an Exception is thrown when trying
            // to delete an already deleted object.
            Assert.Throws<KeyNotFoundException>(() => p.Delete());
        }

    }
}
